package com.itqf.Controller;

import com.itqf.Service.User.UserService;
import com.itqf.Util.JwtUtils;
import com.itqf.entity.User;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.HttpResource;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController
{
    @Resource
    private UserService userService;

    //选择用户年级，选择完成后根据年级跳转到首页
    @RequestMapping(value = "/choicegread",method = RequestMethod.GET)
    //@ResponseBody
    public void choicegread(int gradeid,HttpServletResponse response,HttpServletRequest request)
    {
        JwtUtils jwtUtils=new JwtUtils();
        User user = jwtUtils.Soluecookie(request);
        /*User user=new User();
        user.setUserid("1");*/
        String userid = user.getUserid();
        response.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter out = response.getWriter();
            userService.updateusergread(userid,gradeid);
            out.print("<script>alert('选择成功');location.href='/homePage?gradeid="+gradeid+"';</script>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/findgrade",method = RequestMethod.GET)
    @ResponseBody
    public List<String> findgrade(HttpServletResponse response,HttpServletRequest request)
    {
        JwtUtils jwtUtils=new JwtUtils();
        User user = jwtUtils.Soluecookie(request);
       /* User user=new User();
        user.setUserid("1");*/
        String userid = user.getUserid();
        //response.setContentType("text/html;charset=utf-8");
        String grade = userService.finduserandgrade(userid);
        List<String> list=new ArrayList<>();
        list.add(grade);
        return list;
    }


}

